import 'package:flutter/material.dart';
import 'package:flutter_ohos/app/common/global.dart';
import 'package:flutter_ohos/app/modules/mine/mine_view.dart';
import 'package:flutter_ohos/app/modules/time_flow/time_flow_view.dart';
import 'package:lottie/lottie.dart';
import 'package:provider/provider.dart';

import 'index_provider.dart';

class IndexPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (BuildContext context) => IndexProvider()..initData(),
      builder: (context, child) => _buildPage(context),
    );
  }

  Widget _buildPage(BuildContext context) {
    final provider = context.read<IndexProvider>();
    final state = provider.state;

    return Scaffold(
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(Global.paddingTop),
        child: Offstage(offstage: true, child: Container()),
      ),
      body: Consumer<IndexProvider>(builder: (context, provider, child) {
        return Container(
          child: SafeArea(
              child: IndexedStack(
            index: state.tabIndex,
            children: [
              TimeFlowPage(),
              MinePage(),
            ],
          )),
        );
      }),
      bottomNavigationBar:
          Consumer<IndexProvider>(builder: (context, provider, child) {
        return BottomNavigationBar(
            currentIndex: state.tabIndex,
            type: BottomNavigationBarType.fixed,
            selectedItemColor: Colors.blue,
            unselectedItemColor: Colors.grey,
            backgroundColor: Colors.white60,
            items: [
              BottomNavigationBarItem(
                icon: state.tabIndex == 0
                    ? Image.asset('assets/images/icon_home_selected.png')
                    : Image.asset('assets/images/icon_home_unselected.png'),
                label: '首页',
              ),
              BottomNavigationBarItem(
                icon: state.tabIndex == 1
                    ? Image.asset('assets/images/icon_mine_selected.png')
                    : Image.asset('assets/images/icon_mine_unselected.png'),
                label: '我的',
              ),
            ],
            onTap: (index) => {provider.updateTabIndex(index)});
      }),
    );
  }
}
